/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.redis;

import com.workcell.workdata.im.component.event.BatchMessageEvent;
import com.workcell.workdata.im.component.event.MakeSessionEvent;
import com.workcell.workdata.im.component.event.TagMessageEvent;
import com.workcell.workdata.im.entity.Session;
import com.workcell.workdata.im.model.dto.BatchMessage;
import com.workcell.workdata.im.model.dto.TagMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SignalRedisTemplate extends StringRedisTemplate {

	@Resource
	private ApplicationContext applicationContext;

	public SignalRedisTemplate(RedisConnectionFactory connectionFactory) {
		super(connectionFactory);
	}

	/**
	 * 消息发送到 集群中的每个实例，获取对应长连接进行消息写入
	 * @param message
	 */
	public void push(BatchMessage message) {

		applicationContext.publishEvent(new BatchMessageEvent(message));

		/*
		集群场景可使用redis 广播
		super.convertAndSend(Common.PUSH_MESSAGE_INNER_QUEUE, JSONUtils.toJSONString(message));
		 */

	}

	/**
	 * 消息发送到 集群中的每个实例，通过tag获取对应长连接列表进行消息写入
	 * @param message
	 */
	public void push(TagMessage message) {

		applicationContext.publishEvent(new TagMessageEvent(message));

		/*
		集群场景可使用redis 广播
		super.convertAndSend(Common.TAG_MESSAGE_INNER_QUEUE, JSONUtils.toJSONString(message));
		 */

	}

	/**
	 * 消息发送到 集群中的每个实例，解决多终端在线冲突问题
	 * @param session
	 */
	public void bind(Session session) {

		applicationContext.publishEvent(new MakeSessionEvent(session));

		/*
		集群场景可使用redis 广播
		super.convertAndSend(Common.BIND_MESSAGE_INNER_QUEUE, JSONUtils.toJSONString(session));
		 */
	}
}
